<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.honkentuber.*" %>
<%@ page import="com.honkentuber.chat.*" %>
<%@ page import="java.util.List" %>

<html><body>
<%
  // TODO(ptucker) do not serve JSP publicly
  Key userKey = (Key) request.getAttribute("userKey");
  MyUserCache userCache = (MyUserCache) request.getAttribute("userCache");
  MyUser user = userCache.getUsers().get(userKey);
  if (user != null) {
    String keyString = KeyFactory.keyToString(user.getKey());
%>
  <p>Hello, <%=user.getNickName()%>! (You can
  <a href="<%= request.getAttribute("logoutUrl") %>">sign out</a>.)</p>
  <a href="roomlist?html=1&creatorKey=<%= keyString %>">My rooms</a><br>
<%
  } else {
%>
  <p>Hello! <a href="<%= request.getAttribute("loginUrl") %>">Sign in</a> to
  include your name with greetings you post.</p>
<%
  }
%>
  <a href="roomlist?html=1">All rooms</a><br>
  <ol>
<%
  for (Room room : (List<Room>) request.getAttribute("rooms")) {
    String roomKey = KeyFactory.keyToString(room.getKey());
    Key creatorKey = room.getCreatorKey();
    String creatorString = (creatorKey == null) ? "" : creatorKey.toString();
%>
    <li>
      <b><a href="chat?html=1&form=1&roomKey=<%= roomKey %>">
        <%= room.getName() %>
      </a></b>
      (created by <%= creatorString %>)
    </li>
<%
  }
%>
  </ol>
<%
  if (user != null) {
%>
  <form action="/newroom?html=1" method="post">
    <label for="name">
      <input id="name" name="name" size="64">
    </label>
    <input type="submit" value="New room" />
  </form>
<%
  }
%>
</body></html>